<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use App\Common\Constants\ErrorCode;
use App\CustHub\Service\CommonService;
use App\Enum\SopTypeEnum;
use App\Exception\BusinessException;
use Hyperf\Database\Model\SoftDeletes;
use Mine\MineModel;
use function Yansongda\Supports\data_get;

/**
 * @property int $id 主键
 * @property int $main_uid 主账号id
 * @property int $uid 私域用户ID
 * @property string $title 标题
 * @property int $is_personal 1个人 2企业
 * @property int $template_type 
 * @property string $return_code 
 * @property string $return_text 
 * @property array $plan 
 * @property int $is_return 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property \Carbon\Carbon $deleted_at
 */
class ChSopTemplate extends MineModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_sop_template';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'main_uid', 'uid', 'title', 'is_personal', 'template_type', 'return_code', 'return_text', 'plan', 'is_return', 'created_at', 'updated_at','deleted_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'main_uid' => 'integer', 'uid' => 'integer', 'is_personal' => 'integer', 'template_type' => 'integer', 'plan' => 'array', 'is_return' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime'];


    public function getPlanAttribute()
    {
        return !empty($this->attributes['plan'])?json_decode($this->attributes['plan'],true):[];
    }

    public static function  saveTemplate($uid,$mainUid,$data = [],$id = 0)
    {
        $model = new self();
        if($id){
            $model = self::where('id',$id) -> first();
            if(empty($model)) throw new BusinessException(ErrorCode::NOT_ISSET,'数据不存在');
        }
        $model -> uid = $uid;
        $model -> title = $data['title'];
        if(!is_array($data['plan'])){
            $data['plan'] = json_decode($data['plan'],true);
        }
        $plan = self::formatConf($data['template_type'],$data['plan']);
        //$plan['send_type'] = $data['plan']['send_type'];
        foreach ($plan as &$p){
            $p['content'] = CommonService::formatMsgContent($p['content'])['content'];
        }
        $model -> plan = $plan;
        $model -> is_personal = $data['is_personal'];
        $model -> template_type = $data['template_type'];
        $model -> is_return = data_get($data,'is_return');
        $model -> return_code = data_get($data,'return_code');
        $model -> return_text = data_get($data,'return_text');
        $model -> main_uid = $mainUid;
        $model -> save();
        return $model;
    }

    /**
     * 保存配置
     */
    public static function  formatConf($type,$conf = [])
    {
        try {
            $newConf = [];
            foreach ($conf as $key => $item){
                $newItemConf = [];
                $newItemConf['send_type'] = $item['send_type'];
                $newItemConf['day'] = $item['day'];
                $newItemConf['content'] = $item['content'];
                if($key==0){
                    $defaultConf = SopTypeEnum::$sopBaseConf[$type];
                }else{
                    $defaultConf = SopTypeEnum::$sopBaseConf[1];
                }
                $subConf = $defaultConf['conf'][$item['send_type']];
                foreach ($subConf as $key1 => $item1){
                    $newItemConf['conf'][$key1] = $item['conf'][$key1];
                }
                $newConf[] = $newItemConf;
            }
            return $newConf;
        }catch (\Exception $e){
            throw new BusinessException(ErrorCode::PARAMS_ERROR,$e -> getMessage());
        }
    }
}
